package juc.executors;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class TimerScheduler {

    /**
     * scheduler solution
     * Timer/TimerTask
     * SchedulerExecutorService
     * Linux crontab : interval correct
     * cron4j
     * quartz石英钟
     *
     */
    public static void main(String[] args) {
        Timer timer = new Timer();
        final TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("==" + System.currentTimeMillis());
                timeSleep(3);
            }
        };
        //Timer Question: when the timerTask process more than 1 seconds what happen?
        //delay in milliseconds
        timer.schedule(task, 1000, 1000);
    }

    private static void timeSleep(long time) {
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
